package nayasis.common.db.manager.sql.condition;

import nayasis.common.db.manager.sql.ParamValue;
import nayasis.common.db.manager.sql.type.Command;
import nayasis.common.db.manager.sql.type.Constant;
import nayasis.common.db.manager.sql.type.LogicalOperator;
import nayasis.common.db.manager.sql.type.Brace;
import nayasis.common.db.manager.sql.type.Type;
import nayasis.common.util.Validator;

public class NodeTmp {

    private Type            type;

    private Command         command;
    private LogicalOperator logicalOperator;
    private Brace        sentence;

    private Constant        constant;
    private ParamValue      constantValue;

    private String          variableName;
    
    public NodeTmp( Command command ) {
        this.command = command;
        this.type    = Type.command;
    }

    public NodeTmp( String value ) {
        
        if( setParam( value ) ) return;
        
        setConstant( value );
        
    }

    public NodeTmp( LogicalOperator logicalOperator ) {
        this.logicalOperator = logicalOperator;
        this.type            = Type.logicalOperator;
    }

    public NodeTmp( Brace sentence ) {
        this.sentence = sentence;
        this.type     = Type.brace;
    }
    
    public boolean is( Type type ) {
        return this.type == type;
    }

    
    private boolean setParam( String value ) {
        
        if( value != null && value.length() != 0 ) {
            
            if( value.charAt(0) == ':' ) {
                
                this.type = Type.variable;
                this.variableName = value.substring( 1 );
                
                return true;
            }
            
        }
        
        return false;
        
    }

    private void setConstant( String value ) {
        
        this.type = Type.constant;
        
        if( Validator.isNumber(value) ) {
            
            this.constantValue = new ParamValue( Integer.parseInt(value) );
            
        } else if( Validator.isNumeric( value ) ) {
            
            this.constantValue = new ParamValue( Double.parseDouble(value) );
            
        } else {
            
            this.constantValue = new ParamValue( value );
            
        }
        
    }
    
}
